package com.cmcc.linlaker.ui.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.cmcc.linlaker.LoginActivity;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.bean.LoginBean;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.adapter.BookAdapter;
import com.cmcc.linlaker.ui.mine.adapter.MessageAdapter;
import com.cmcc.linlaker.ui.mine.bean.BookBean;
import com.cmcc.linlaker.ui.mine.bean.MessageBean;
import com.cmcc.linlaker.ui.query.adapter.QueryPageAdapter;
import com.cmcc.linlaker.ui.upload.UploadHistoryActivity;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.utils.Utils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        BarUtils.initStatusBar(this);

        initView();
    }

    private RecyclerView recycler_message;
    private MessageAdapter adapter;
    private List<MessageBean.MessageListBean> itemList = new ArrayList<>();

    private MaterialRefreshLayout refreshLayout;

    private void initView() {
        recycler_message = findViewById(R.id.recycler_message);
        setRecyclerView(recycler_message, itemList);

        refreshLayout = findViewById(R.id.refresh);
        refreshLayout.setLoadMore(true);
        refreshLayout.setWaveShow(true);
        refreshLayout.setWaveColor(Color.parseColor("#3eaefc"));

        refreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        reset(false);
                    }
                }, 2000);
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                if (isHasLoaded) {
                    ToastUtils.showText(MessageActivity.this, "数据已全部加载");
                    refreshLayout.finishRefreshLoadMore();
                } else {
                    getMsgList(false);
                }
            }
        });
    }

    private int pageSize = 20;
    private int pageNum = 1;
    private boolean isHasLoaded = false;

    private void reset(boolean isShowDialog) {
        itemList.clear();
        pageNum = 1;
        isHasLoaded = false;
        getMsgList(isShowDialog);
    }

    @Override
    protected void onResume() {
        super.onResume();

        reset(true);
    }

    private void setRecyclerView(final RecyclerView recycler_view, final List<MessageBean.MessageListBean> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);
        adapter = new MessageAdapter(this, itemList);
        recycler_view.setAdapter(adapter);
    }

    /**
     * 消息列表
     */
    private void getMsgList(boolean isShowDialog) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("receiveType", "1");
        params.put("receiveCodes", SPHelper.getUid());
        params.put("pageSize", pageSize + "");
        params.put("pageNum", pageNum + "");
        params.put("isAsc", "asc");

        helper.sendPOSTRequest(ApiDefine.MSG_LIST, MessageBean.class, params, isShowDialog, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    MessageBean bean = (MessageBean) data;

                    if (bean.code.equals("0")) {
                        List<MessageBean.MessageListBean> list = bean.rows;

                        if (list.size() == pageSize) {
                            itemList.addAll(bean.rows);
                            adapter.notifyDataSetChanged();
                            pageNum++;
                        } else {
                            itemList.addAll(bean.rows);
                            adapter.notifyDataSetChanged();
                            isHasLoaded = true;
                        }
                        refreshLayout.finishRefresh();
                        refreshLayout.finishRefreshLoadMore();

                        if (itemList.size() == 0) {
                            refreshLayout.setVisibility(View.GONE);
                            findViewById(R.id.layout_no_msg).setVisibility(View.VISIBLE);
                        } else {
                            refreshLayout.setVisibility(View.VISIBLE);
                            findViewById(R.id.layout_no_msg).setVisibility(View.GONE);
                        }
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(MessageActivity.this);
                    }
                }
            }
        });
    }

    public void back(View view) {
        finish();
    }

}
